//SnowballKillListener.java

package qwq.vincentzyu.Event;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Egg;
import org.bukkit.entity.Player;
import org.bukkit.entity.Snowball;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import qwq.vincentzyu.JPTTPlugin;

/**
 * Created by Administrator on 2025/3/13.
 */
public class SnowballKillListener implements Listener {
    private final JPTTPlugin plugin;

    public SnowballKillListener(JPTTPlugin plugin){
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onSnowballThrow(ProjectileLaunchEvent event){
        if ( !(event.getEntity().getShooter() instanceof Player) ) return;

        if ( event.getEntity() instanceof Snowball ){

            if (!plugin.isSnowballKillEnabled()) return;

            Player player = (Player) event.getEntity().getShooter();

            player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0f, 1.0f);

            Bukkit.getScheduler().runTaskLater(this.plugin, ()-> {
                Location spawnLoc = player.getRespawnLocation() != null ?
                        player.getRespawnLocation() :
                        player.getWorld().getSpawnLocation();

                player.sendMessage("§a雪球紫砂成功");  // 绿色文字
                player.setHealth(0);
                player.teleport(spawnLoc);
            }, 2L);
        } else if ( event.getEntity() instanceof Egg ){
            if ( !plugin.isEggReturnEnabled() ) return;

            Player player = (Player) event.getEntity().getShooter();
            Location loc = plugin.getReturnLocation().clone();

            player.playSound(player.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 1.0f, 1.0f);


            Bukkit.getScheduler().runTaskLater(this.plugin, ()-> {
                player.teleport(loc);
                player.sendMessage(String.format("§a传送完成 §7已返回指定坐标 %.1f, %.1f, %.1f", loc.getX(), loc.getY(), loc.getZ()));  // 绿色文字
//            player.spigot().sendMessage(
//                ChatMessageType.ACTION_BAR,
//                TextComponent.fromLegacyText(
//                    String.format("§6传送完成 §7已返回指定坐标 %.1f, %.1f, %.1f", loc.getX(), loc.getY(), loc.getZ())
//                )
//            );
            }, 3L);
        }

    }
}
